import 'dart:async';
import 'package:flutter/material.dart';

// ignore: must_be_immutable
class NetLoadingDialog extends StatefulWidget {
  String loadingText;
  bool outsideDismiss;

  Function dismissDialog;

  bool showLoading;
  NetLoadingDialog(
      {Key? key,
      this.loadingText = '加载中',
      this.outsideDismiss = true,
      required this.dismissDialog,
      this.showLoading = true})
      : super(key: key);

  @override
  State<NetLoadingDialog> createState() => _NetLoadingDialog();
}

class _NetLoadingDialog extends State<NetLoadingDialog> {
  bool isDismiss=false;
  _dismissDialog() {
//    ModalRoute route = ModalRoute.of(context);
     if(!isDismiss) {
       Navigator.of(context).pop();
       isDismiss=true;
     }
  }

  @override
  void initState() {
    super.initState();
//
//     widget.dismissDialog(
//         //将关闭 dialog的方法传递到调用的页面.
//         () {
//           if(!isDismiss) {
//             Navigator.of(context).pop();
//             isDismiss=true;
//           }
//       //如果当前路由是最顶层路由才pop回上一层
// //        ModalRoute route = ModalRoute.of(context);
// //        if (route.isCurrent) Navigator.of(context).pop();
//     });
  }

  @override
  Widget build(BuildContext context) {
    return widget.showLoading
        ? GestureDetector(
            onTap: widget.outsideDismiss ? _dismissDialog : null,
            child: Material(
              type: MaterialType.transparency,
              child: new Center(
                child: new SizedBox(
                  width: 120.0,
                  height: 120.0,
                  child: new Container(
                    decoration: ShapeDecoration(
                      color: Color(0xffffffff),
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.all(
                          Radius.circular(8.0),
                        ),
                      ),
                    ),
                    child: new Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: <Widget>[
                        new CircularProgressIndicator(),
                        new Padding(
                          padding: const EdgeInsets.only(
                            top: 20.0,
                          ),
                          child: new Text(
                            widget.loadingText,
                            textScaleFactor: 1.0,
                            style: new TextStyle(fontSize: 12.0),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            ),
          )
        : Container();
  }
}
